const { Random } = require('mockjs')
const Mock = require('mockjs')

const data = Mock.mock({
  'items|5': [{
    'id|+1': 0,//id int
    'name|+1': ['管理员', '普通用户', '测试人员', '开发人员', 'HR'],//角色名 string
    code: '@string("number", 4)',//角色编号 string
    companyId: 0,//公司id int
    departmentId: '@integer(0,4)',//部门id int
    note: '@csentence(5,10)',//备注 string
    state: '@integer(0,1)',//状态 int(0：停用，1：启用)
    resources: [],//资源列表 List<resourceId>
  }]
})

module.exports = [
  //用户列表获取角色
  {
    url: '/api/role/getRoles',
    type: 'get',
    response: config => {
      const items = data.items
      return {
        code: 20000,
        data: {
          total: items.length,
          items: items
        }
      }
    }
  },
  //角色列表获取列表
  {
    url: '/api/role/list',
    type: 'get',
    response: config => {
      const items = data.items

      const { name, pageSize, pageNum } = config.query
      const mockList = items.filter(role => {
        if (!name || role.name == name) return true
        return false
      })
      const pageList = mockList.filter((item, index) => index < pageSize * pageNum && index >= pageSize * (pageNum - 1))
      return {
        code: 20000,
        data: {
          total: mockList.length,
          items: pageList
        }
      }
    }
  },
  //根据code查找角色
  {
    url: '/api/role/queryByCode',
    type: 'get',
    response: config => {
      const { code } = config.query
      let find = null
      data.items.forEach(item => {
        if (item.code == code) {
          find = { ...item }
          return
        }
      })
      return {
        code: 20000,
        data: {
          role: find
        }
      }
    }
  },
  //新增角色
  {
    url: '/api/role/addRole',
    type: 'post',
    response: config => {
      const { role } = config.body
      role.id = data.items[data.items.length - 1].id + 1
      role.companyId = 0
      data.items.push(role)
      return {
        code: 20000,
        message: "添加成功"
      }
    }
  },
  //修改角色
  {
    url: '/api/role/updateRole',
    type: 'post',
    response: config => {
      const { role } = config.body
      data.items.forEach((item, index) => {
        if (item.id == role.id) {
          data.items.splice(index, 1, role)
        }
      })
      return {
        code: 20000,
        message: "修改成功"
      }
    }
  },
  //删除角色
  {
    url: '/api/role/deleteById',
    type: 'get',
    response: config => {
      const { id } = config.query
      data.items = data.items.filter(role => role.id != id)
      return {
        code: 20000,
        message: "删除成功"
      }
    }
  },
]
